package com.onlineMIS.ORM.DAO.chainS.chainMgmt;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.onlineMIS.ORM.DAO.BaseDAO;
import com.onlineMIS.ORM.DAO.headQ.barCodeGentor.QuarterDaoImpl;
import com.onlineMIS.ORM.entity.chainS.chainMgmt.QxbabyConf;
import com.onlineMIS.ORM.entity.headQ.barcodeGentor.Quarter;
import com.onlineMIS.common.Common_util;


@Repository
public class QxbabyConfDaoImpl extends BaseDAO<QxbabyConf> {
	
	    @Autowired
	    private QuarterDaoImpl quarterDaoImpl;
	    public Set<Integer> getCurrentYear() {
	    	Set<Integer> yearIdSet = new HashSet<Integer>();
	    	
	    	QxbabyConf qxbabyConf = getConf("CURRENT_YEAR");
	    	if (qxbabyConf != null) {
	    		String yearIdString = qxbabyConf.getValue();
	    		Common_util.string2IntSet(yearIdString,yearIdSet);
	    	} 
	    	return yearIdSet;
	    }
	    
	    public Integer getLatestYear() {
	    	Set<Integer> yearIdSet = getCurrentYear();
	    	int yearId = 0;
	    	for (Integer yearInteger : yearIdSet) {
	    		if (yearInteger > yearId)
	    			yearId = yearInteger;
	    	}
	    	return yearId;
	    }
	    
	    public Set<Integer> getCurrentQuarter() {
	    	Set<Integer> quarterIdSet = new HashSet<Integer>();
	    	
	    	QxbabyConf qxbabyConf = getConf("CURRENT_QUARTER");
	    	if (qxbabyConf != null) {
	    		String quarterString = qxbabyConf.getValue();
	    		Common_util.string2IntSet(quarterString,quarterIdSet);
	    	} 
	    	return quarterIdSet;
	    }
	    
	    public Integer getLatestQuarter() {
	    	Set<Integer> quarterIdSet = getCurrentQuarter();
	    	List<Quarter> quarters = quarterDaoImpl.getQuarters(quarterIdSet);
	    	Quarter currentQuarter = null;
	    	for (Quarter quarter : quarters) {
	    		if (currentQuarter == null || quarter.compareTo(currentQuarter) > 0)
	    			currentQuarter = quarter;
	    	}
	    	return currentQuarter.getQuarter_ID();
	    }
	
		private QxbabyConf getConf(String parmKey){
			DetachedCriteria criteria = DetachedCriteria.forClass(QxbabyConf.class);
			criteria.add(Restrictions.eq("parmKey", parmKey));

			List<QxbabyConf> confs = this.getByCritera(criteria, true);
			if (confs == null || confs.size() == 0)
				return null;
			else 
				return confs.get(0);
		}
		
		
}
